HotBasic for Beginners - Chapter 2
HotBasic for New Coders Even if you have never written a line of code, you can program now in HotBasic! Chapter Two - More basics, by lBl In chapter one we learned the meaning and use of a few of the terms necessary to understand how to code. These were OOP, Form, Object (an alias for Control), Events, Subroutines, Functions, ShowMessage, Create, ShowModal, End, IDE, IP, Compiler and Text Editor. If you forgot the meaning of any of these words, its best you go back to Chapter One and re read, as these terms are essential building blocks upon which the following concepts will develop. Now we are going to expand on this list and develop more details. We will begin by embelishing the code illustrated below from chapter one. '---begin code--------- $APPTYPE GUI '---the type of output file we want (an EXE) Declare Sub Saythis '---code will contain this subroutine CREATE Form as Form '---a window object is created Create B1 as Button '---a button is created inside the form Onclick = Saythis '---buttons Event is to run sub Saythis End Create '---end of buttons properties End Create '---end of Form create Form.ShowModal '---instruct the OS to display the Form Sub Saythis '---begin Saythis subroutine SHOWMESSAGE "Hello World!" '---a method to display text End Sub '---close this sub '---end code----------- Now lets explore the things we can do with Form to add more whistles and bells. '---begin embellished code--------- $APPTYPE GUI '---output file we want (an EXE) Declare Sub Saythis '---code will contain this subroutine Declare Sub min_close '---code will contain this subroutine Declare Sub message_handler '---code will contain this subroutine CREATE Form as Form '---a window object is created Top = 200 '---vertical start pixel pos (Y axis) Left = 200 '---horizontal start pixel pos(X axis) Width = 400 '---width in pixels of form Height = 300 '---height in pixels of form ShowHint = True '---allow mouseover tip popups Icon="pnote.ico" '---application Icon Caption="Hello Copyright 2008 by K5HUM" '---Header message on form OnClose = min_close '---take special action on Exit onmessage = message_handler '---monitor OS internal messages Create B1 as Button '---a button is created in the form Caption = "Message #1" '---title on the button Hint = "Click to show message" '---mouseover help event Onclick=Saythis '---Event is to run sub Saythis End Create '---end of buttons properties End Create '---end of Form create Form.ShowModal '---instruct the OS to display Form Sub Saythis '---begin Saythis subroutine SHOWMESSAGE "Hello World!" '---display msg in box End Sub '---close this sub Sub min_close '---begin min_close sub (your own method code here) '---what you want before exiting End Sub '---close this sub Sub message_handler '---begin message_handler sub (your own method code here) '---look for a WinMsg and act on it End Sub '---close this sub '---end code----------- Before I proceed to discuss some of the new code I added above, I want to mention that one of the great flexibilities of Windows programming in general is the variety of ways any single task or job can be coded. One example is the use of "Dim Form as Form" in place of Create. The use of Create in HB was to maintain some compatibility with older Basic compilers, but as you will learn, the newer "Dim" cuts down on typing overhead. A great help when writing your code is to use very verbose descriptions for the names of your controls. Example, in a simple program, B1 would be ok to describe a button, but in a program with many buttons B1, B2 etc would really test your memory as to their purpose. HB cares not how long a description is, so, in your code you could name the B1 button something like DisplayHelloWorldToUser. The same general rule applies to naming subroutines, variables, constants etc. Remember too, that in HB, the precise case used in an identifier frequently matters. Regarding where to position your GUI on a users desktop, keep in mind that the numerical value in pixels you use is a function of how the user has his default screen resolution set. The starting point for the very center of the screen on an 800(X)x600(Y) pixel screen would be Left 400 and Top of 300. However, IF your Form is say 200 pixels wide and 100 pixels deep, you need to compensate and have Top=200 and Left=200. Notice now that if Top starts at 200 and is 200 pixels deep, the form ends at 400. Therefore your form sits in the middle height wise with 200 pixels above and below it. Likewise it is then horizontally centered. If the user has his screen resolution set lower or higher, the operating system will paint your form bigger or smaller on those desktops. A form by default, can be resized (by dragging the sides in and out with the mouse) and moved by clicking, holding and moving the blue header bar, by the user. The resize feature can be turned off with another form propertie, Form.Resizeable = false. Its good to learn to calculate these thing but eventually you may want something called a Form or GUI Builder. A GUI or Form builder will allow you to drag and drop Objects on a template, positioning and sizing them as desired, then do the tedious work of generating the code. A good Form builder will not only create Object (or Control) code, but will calculate object positions and provide object Properties code as well. It will also automatically set the code into your project. Personally I don't use a Form builder as HB never came with one (and still doesn't); but the upside was that I learned to do the position math and code forms and controls etc from scratch. There is probably a Windows API (a code routine called the Application Programmers Interface) to determine the users video card setting (or screen resolution) and have your code automatically compensate when instructing the OS to paint the form to size. ShowHint is a very useful property of a Form. You can literally create instant Help. This is invaluable in todays world where few users bothers to read program documentation. When ShowHint=True in a form, you can create a text mouseover event for almost all of the Forms objects or controls. Oh, from here on I will use the alias Control when referencing Objects. You simply add Hint="your tip or clue" inside the specific control, as illustrated in the above embellished code. Icon="xyz.ico". An Icon can help define your program and set it apart, very much like the outline of a skirt on a door indicates a women's restroom. Icons with the .ICO extension are small graphic images designed for use in Windows programs. They must meet certain pixel dimensions and format specification. Windows uses a programs internal icon to, among other things, create shortcuts from which to launch or run your application. If a program is created without one, the OS will substitute a plain jane one. There are Icon Editors that can be used to edit or create them. However, you must have some artistic ability. Fortunately there are a jillion Icons out in the public domain that you can choose from. Caption="Hello Copyright 2008 by K5HUM" is an example of a message banner most want in the top header of your program window. Program version info, Authors name and copyright info are commonly placed there. If you havent figgured it out already, K5HUM is my Ham Radio station call. OnClose=min_close This is an event that triggers a subroutine when a user clicks the "X" in the top right corner of a program window. Example, before closing you might want to save something; or not close at all and do something else. onmessage=message_handler This is a special event that triggers a sub anytime the OS spits out one of its zillion messages. The Windows OS literally spits out a message for almost anything that happens. You can trap and examine these messages for further processing. Example, if a user moves the GUI, you could put up a message warning them not to do it. Or you could look to see if some keys were pressed. This is a very powerful feature and a great deal of coding care must be exercised when using it. More on this later. The Form object has many other interesting properties for the coder to explore. Might I suggest at this point a nice 3rd party HB documentation utility called "HotBasic Assistant" created by Phillip Bister, and available for download at: http://www.philipbister.ukonline.co.uk/HBassistant.zip Philip's HB Assistant is essentially a nice electronic searchable manual for HB, although a bit condensed. REVIEW: We covered form positioning, form builders, the use of mouseover events like pop up hints, program icons, window header captions, special closing routines and trapping windows messages. In the next chapter we will add to and discuss the use of Menus, Status messages, and text edit boxes in our helloworld form.